package com.miyake.client.profile;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;
import com.miyake.client.tools.ClickableImage;
import com.miyake.client.tools.ClickableLabel;
import com.miyake.client.tools.ImageListElement;
import com.miyake.client.tools.SmallButton;

public class UserListDialog2 extends DialogBox {
	private DoctorsUserServiceAsync service = DoctorsUserService.Util.getInstance();

	public UserListDialog2() {	
		setHTML("Users");
		
		VerticalPanel verticalPanel = new VerticalPanel();
		setWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");
		getUserList(verticalPanel);
	}
	
	private void getUserList(final VerticalPanel verticalPanel) {
		service.getAllUsers(new AsyncCallback<List<ImageListElement>>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<ImageListElement> result) {
				showList(result, verticalPanel);
			}
		});
	}

	protected void showList(List<ImageListElement> result,
			VerticalPanel verticalPanel) {
		
		final Iterator<ImageListElement> it = result.iterator();
		ThumbNail thumbNail = new ThumbNail(result.size(), 1) {
			@Override
			protected Widget getWidget(final int i) {
				return createElement(i, it.next());
			}
		};
		verticalPanel.add(thumbNail);
		thumbNail.show();
		verticalPanel.add(new SmallButton("Close", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide(false);
			}
		}));
	}

	protected Widget createElement(int i, final ImageListElement next) {
		HorizontalPanel h = new HorizontalPanel();
		Image image = new ClickableImage();
		image.setSize("80px", "80px");
		if (next.getImageURL() != null) {
			image.setUrl(next.getImageURL());
		}
		image.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				requestFriend(next.getKey());
			}
		});
		h.add(image);
		final VerticalPanel v = new VerticalPanel();
		h.add(v);
		v.add(new ClickableLabel(next.getName()));
		v.add(new SmallButton("Friend Request", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				requestFriend(next.getKey());
			}
		}));
		final VerticalPanel detailPanel = new VerticalPanel();
		v.add(new ClickableLabel("Detail>>", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if ( ((ClickableLabel)event.getSource()).getText().equals("Detail>>")) {
					((ClickableLabel)event.getSource()).setText("Hide<<");
					
					detailPanel.clear();
					showDetail(next.getKey(), detailPanel);
					v.add(detailPanel);
				}
				else {
					((ClickableLabel)event.getSource()).setText("Detail>>");
					v.remove(detailPanel);
				}
			}
		}));
		return h;
	}

	protected void requestFriend(String key) {
		service.addFriend(key, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Void result) {
				hide(false);
			}
			
		});
	}

	protected void showDetail(String key, final VerticalPanel v) {
		service.getUser(key, new AsyncCallback<UserProperty>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(UserProperty result) {
				for (UserElement e : result.getProperties()) {
					if (!e.getValue().isEmpty()) {
						v.add(new Label(e.getTitle() + ":" + e.getValue()));
					}
				}
			}
		});
	}
}
